<?php
/*
	$table		bang de insert
	$labels		danh sach cac nhan se hien thi tuong ung voi moi truong, chi so phai bat dau tu 0
	$columns	danh sach cac cot se hien thi, neu khong cho tham so nay se hien thi tat ca,
					chi so phai bat dau tu 0
	$imagecolumns		danh sach cac truong la duong dan file anh
	$thumbnailcolumns	danh sach cac cot la thumbnail cua file anh
	$combocolumns		la mang 2 chieu, moi phan tu co key la ten cot, value la mot mang chua cac phan tu cua combo
*/
$CI = &get_instance();
$CI->load->helper('html');
$CI->load->helper('form');
$CI->load->database();
$query = $CI->db->query('show columns from ' . $table);
$allColumns = $query->result();
$colProp = array();
foreach($query->result_array() as $rr)
{
	$colProp[$rr['Field']] = $rr;
}

if(!isset($columns))
{
	foreach($allColumns as $row)
		$columns[] = $row->Field;
}
if(!isset($labels))
	foreach($columns as $field)
		$labels[] = $field;
if(count($labels) < count($columns))
{
	$n1 = count($labels);
	$n2 = count($columns);
	for($i=0; $i<$n2-$n1; $i++)
		$labels[] = $columns[$n1+$i];
}

function getColumnProp($column, &$allColumns)
{
	foreach($allColumns as $r)
		if($column == $r->Field)
			return $r;
}
function isImageColumn($column, &$imagecolumns)
{
	if(!isset($imagecolumns))
	{
		return FALSE;
	}
	else
	{
		foreach($imagecolumns as $r)
			if($r == $column)
				return TRUE;
	}
	return FALSE;
}
function isThumbnailColumn($column, &$thumbnailColumns)
{
	if(!isset($thumbnailColumns))
		return FALSE;
	if(isset($thumbnailColumns[$column]))
		return TRUE;
	return false;
}
function isComboColumn($column, &$combocolumns)
{
	if(!isset($combocolumns))
		return FALSE;
	else
	{
		foreach($combocolumns as $key=>$value)
			if($key == $column)
				return TRUE;
	}
	return FALSE;
}
?>
<script type="text/javascript" src="javascript/admin/script.js">
</script>
<div>
<?php echo validation_errors(); ?>
</div>
<div>
<table width="100%">
<?php
	echo form_open_multipart('admin/quanly/thuchienthem/'.$table, array('name'=>$table, 'id'=>$table));
	$n = count($columns);
	for($i=0; $i<$n; $i++):?>
		<tr>
        	<td>
				<div><a class="link">
                	<?php 
						if(!isThumbnailColumn($columns[$i], $thumbnailcolumns))
							echo $labels[$i]; 
					?>
                    </a>
				</div>
				<div>
                	<?php 
						$prop = getColumnProp($columns[$i],$allColumns);
						if($prop->Extra == 'auto_increment')
						{
							$data = array(
										  'name'=>$columns[$i],
										  'id'=>$columns[$i],
										  'maxlength'=>'15',
										  'size'=>'20',
										  'disabled'=>"disabled"
										  );
							echo form_input($data);
						}
						else if(isImageColumn($columns[$i],$imagecolumns))
						{
							echo form_upload(array('name'=>$columns[$i],'id'=>$columns[$i]));
						}
						else if(isThumbnailColumn($columns[$i], $thumbnailcolumns))
						{
						}
						else if(isComboColumn($columns[$i], $combocolumns))
						{
							$option = $combocolumns[$columns[$i]];
							if($colProp[$columns[$i]]['Default'] != NULL)
								$default = set_combo_value($columns[$i], $colProp[$columns[$i]]['Default']);
							else
								$default = set_combo_value($columns[$i]);
							echo form_dropdown($columns[$i], $option, $default);
						}
						else if(strpos($prop->Type,'int') === 0)
						{
							$data = array(
										  'name'=>$columns[$i],
										  'id'=>$columns[$i],
										  'maxlength'=>'15',
										  'size'=>'20'
										  );
							
							if($colProp[$columns[$i]]['Default'] != 'NULL' && $colProp[$columns[$i]]['Default'] != NULL)
								$data['value'] = set_value($columns[$i], $colProp[$columns[$i]]['Default']);
							else
								$data['value'] = set_value($columns[$i]);
							
							echo form_input($data);
						}
						else if(strpos($prop->Type,'tinyint') === 0)
						{
							if($colProp[$columns[$i]]['Default'] != 'NULL' && $colProp[$columns[$i]]['Default'] != NULL)
							{
								$default = set_checkbox($columns[$i], $columns[$i]);
								if(!isset($_POST['submit_'.$table]))
									$default = $colProp[$columns[$i]]['Default'];
							}
							else
							{
								$default = set_checkbox($columns[$i], $columns[$i]);
							}
								
							
							echo form_checkbox($columns[$i], $columns[$i], $default);
						}
						else if(strpos($prop->Type,'bigint') === 0)
						{
							$data = array(
										  'name'=>$columns[$i],
										  'id'=>$columns[$i],
										  'maxlength'=>'20',
										  'size'=>'25'
										  );
							if($colProp[$columns[$i]]['Default'] != 'NULL' && $colProp[$columns[$i]]['Default'] != NULL)
								$data['value'] = set_value($columns[$i], $colProp[$columns[$i]]['Default']);
							else
								$data['value'] = set_value($columns[$i]);
							echo form_input($data);
						}
						else if(strpos($prop->Type,'varchar') === 0)
						{
							$data = array(
										  'name'=>$columns[$i],
										  'id'=>$columns[$i],
										  'size'=>'50'
										  );
							if($colProp[$columns[$i]]['Default'] != 'NULL' && $colProp[$columns[$i]]['Default'] != NULL)
								$data['value'] = set_value($columns[$i], $colProp[$columns[$i]]['Default']);
							else
								$data['value'] = set_value($columns[$i]);
							echo form_input($data);
						}
						else if(strpos($prop->Type,'text') === 0)
						{
							echo '<input type="button" value="Chèn ảnh" onclick="openUploadImageForm(\''.$columns[$i].'\',\''. site_url('admin/quanly/uploadimage') .'\');"/>';
							$data = array(
										  'name'=>$columns[$i],
										  'id'=>$columns[$i],
										  'rows'=>'10',
										  'cols'=>'80'
										  );
							if($colProp[$columns[$i]]['Default'] != 'NULL' && $colProp[$columns[$i]]['Default'] != NULL)
								$data['value'] = set_value($columns[$i], $colProp[$columns[$i]]['Default']);
							else
								$data['value'] = set_value($columns[$i]);
							echo form_textarea($data);
						}
						else if(strpos($prop->Type,'datetime') === 0)
						{
							$data = array(
										  'name'=>$columns[$i]
									);
							if($colProp[$columns[$i]]['Default'] != 'NULL' && $colProp[$columns[$i]]['Default'] != NULL)
							{
								$default = $colProp[$columns[$i]]['Default'];
								$temp = split(' ', $default);
								$temp1 = split('-', $temp[0]);
								$temp2 = split(':', $temp[1]);
								$data['value'] = set_form_date_value($columns[$i], mktime($temp2[0], $temp2[1], $temp2[2], $temp1[1], $temp1[2], $temp1[0]));
							}
							else
								$data['value'] = set_form_date_value($columns[$i]);
							echo form_date($data);
						}
					?>
        		</div>
			</td>
        </tr>
<?php 
	endfor;
?>
		<tr>
        	<td>
            	<div align="center">
                	<?php
					$param = array();
					$param['name'] = 'submit_' . $table;
					$param['id'] = 'submit_' . $table;
					$param['value'] = 'Đồng ý';
					echo form_submit($param);
					echo form_reset(array('name'=>'reset','id'=>'reset','value'=>'Làm lại'));
					?>
                </div>
            </td>
        </tr>
<?php
	echo form_close();
?>
</table>
</div>